package com.example.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    int length;  //蛇的长度
    int[] snakeX = new int[600];  //蛇的x坐标
    int[] snakeY = new int[500];  //蛇的y坐标
    String fx;  //蛇头的方向
    boolean isStart = false;  //判断游戏是否开始
    Timer timer = new Timer(100, this);   //定时器 0.1秒动一次

    //定义食物
    int foodX;
    int foodY;
    Random random = new Random();

    //失败标识
    boolean isFail = false;

    //计分系统
    int score;

    //构造器
    public GamePanel() {
        init(); //初始化

        //获取键盘的监听事件
        this.setFocusable(true);  //使键盘聚焦在游戏中
        this.addKeyListener(this); //监听键盘
        timer.start();
    }


    //初始化数据
    public void init() {
        length = 3;  //初始长度 3节

        snakeX[0] = 100;
        snakeY[0] = 100;  //初始头部坐标

        snakeX[1] = 75;
        snakeY[1] = 100;  //初始第一节身体坐标

        snakeX[2] = 50;
        snakeY[2] = 100;  //初始第二节身体坐标

        fx = "R";  //蛇头初始向右

        //初始化食物位置(随机)
        foodX = 25 + 25 * random.nextInt(34);
        foodY = 75 + 25 * random.nextInt(24);

        score = 0; //初始化计分
    }


    //Graphics g 画笔
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);  //清屏

        setBackground(Color.WHITE);

        //绘制游戏区域
        g.fillRect(25, 75, 850, 600);


        //画一条静态的蛇
        if (fx.equals("R")) {
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]); //头向右
        } else if (fx.equals("L")) {
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]); //头向左
        } else if (fx.equals("U")) {
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]); //头向上
        } else if (fx.equals("D")) {
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]); //头向下
        }

        for (int i = 1; i < length; ++i) {
            Data.body.paintIcon(this, g, snakeX[i], snakeY[i]); //第i节身体，通过length来控制

        }


        //画食物
        Data.food.paintIcon(this, g, foodX, foodY);

        //画积分
        g.setColor(Color.DARK_GRAY);
        g.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g.drawString("长度：" + length, 25, 35);
        g.drawString("分数：" + score, 25, 50);


        //游戏提示：是否开始
        if (!isStart) {
            //画文字
            g.setColor(Color.WHITE);  //画笔颜色
            g.setFont(new Font("微软雅黑", Font.BOLD, 40)); //设置字体
            g.drawString("按下空格开始游戏", 300, 300);
        }

        //失败提醒
        if (isFail) {
            //画文字
            g.setColor(Color.RED);  //画笔颜色
            g.setFont(new Font("微软雅黑", Font.BOLD, 40)); //设置字体
            g.drawString("游戏结束，按下空格重新开始", 200, 300);
        }

    }

    //监听键盘的输入
    @Override
    public void keyPressed(KeyEvent e) {   //键盘按下未释放
        //获取按下的某个键
        int keyCode = e.getKeyCode();

        if (keyCode == KeyEvent.VK_SPACE) {  //若按下空格
            if (isFail) {  //若失败则重新开始
                isFail = false;
                init();
            } else {
                isStart = !isStart;  // 则启动/停止游戏
            }
            repaint();  //刷新界面
        }

        //通过键盘控制方向
        if (keyCode == KeyEvent.VK_LEFT) {
            fx = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            fx = "R";
        } else if (keyCode == KeyEvent.VK_UP) {
            fx = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            fx = "D";
        }


    }


    //定时器，执行定时操作
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && isFail == false) {    //若游戏处于开始状态,且没有失败
            //右移
            for (int i = length - 1; i > 0; i--) {  //除了头部，身体都往前移动
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }

            //通过控制方向使头部移动
            if (fx.equals("R")) {
                snakeX[0] = snakeX[0] + 25;  //头部右移
                //边界判断
                if (snakeX[0] > 850) {   //遇到边界失败
                    //snakeX[0] = 25;//遇到边界回到起点
                    isFail = true;
                }
            } else if (fx.equals("L")) {
                snakeX[0] = snakeX[0] - 25;  //头部左移
                //边界判断
                if (snakeX[0] < 25) {   //遇到边界失败
                    // snakeX[0] = 850;//遇到边界回到起点
                    isFail = true;
                }
            } else if (fx.equals("U")) {
                snakeY[0] = snakeY[0] - 25;  //头部上移
                if (snakeY[0] < 75) {   //遇到边界失败
                    // snakeY[0] = 650;//遇到边界回到起点
                    isFail = true;
                }
            } else if (fx.equals("D")) {
                snakeY[0] = snakeY[0] + 25;  //头部下移
                if (snakeY[0] > 650) {   //遇到边界失败
                    //snakeY[0] = 75; //遇到边界回到起点
                    isFail = true;
                }
            }

            //当蛇头和食物重合
            if (snakeX[0] == foodX && snakeY[0] == foodY) {
                length++;
                score = score + 10;
                //重新生成食物
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 75 + 25 * random.nextInt(24);
            }

            //结束判断
            for (int i = 1; i < length; i++) {
                if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {   //若头部和身体重合
                    isFail = true;
                }

            }


            repaint();  //刷新界面

        }
        timer.start(); //定时器启动
    }

    @Override
    public void keyTyped(KeyEvent e) {   //键盘按下弹起

    }


    @Override
    public void keyReleased(KeyEvent e) {   //释放某个键

    }
}
